import 'package:bruno/bruno.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/utils/DialogUtils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/ImagesGridviewWidget.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';
import 'package:image_pickers/image_pickers.dart';

import 'problem_report_logic.dart';

/**
 * @Description: 随手拍 问题上报
 * @Author: 下雨了
 * @CreateDate: 2022年5月31日 13:59:55
 * @Version: 1.0
 */
///
class ProblemReportPage extends StatelessWidget {
  final String title;
  const ProblemReportPage({Key? key, required this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.put(ProblemReportLogic());
    final state = Get.find<ProblemReportLogic>().state;

    return GetBuilder<ProblemReportLogic>(builder: (logic) {
      return WillPopScope(
        onWillPop: () async {
          if (logic.isOnWillPop()) {
            dialogUtils.show(
              context: context,
              title: "温馨提示",
              content: "是否暂存本次上报内容，稍后继续进行编辑",
              callbackOk: () {
                Navigator.of(context).pop('ok');
              },
              callbackCancel: () {
                logic.deleteData();
                Navigator.of(context).pop('ok');
              }
            );
          }
          return true;
        },
        child: Scaffold(
          backgroundColor: MyColors.FFF1F1F1,
          appBar: MyAppBar(context, title: "随手拍问题上报"),
          body: GestureDetector(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Container(
                    width: Get.width,
                    padding: EdgeInsets.only(left: 16.r, right: 16.r, top: 16.5.r, bottom: 24.5.r),
                    decoration: BoxDecoration(
                        color: Colors.white
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text("图片/视频证据", style: TextStyle(color: MyColors.FF666666, fontSize: 14.sp),),
                        SizedBox(height: 12.5.r,),
                        ImagesGridviewWidget(
                          key: imagesGridViewKey,
                          resultList: logic.imagesList,
                          onMediaResultsCallback: (List<Media> imagesList, filePathList) {
                            logic.imagesList = imagesList;
                            logic.updateData();
                          },
                        ),
                      ],
                    ),
                  ),
                  Container(
                    width: Get.width,
                    decoration: BoxDecoration(
                        color: Colors.white
                    ),
                    margin: EdgeInsets.only(top: 8.r),
                    padding: EdgeInsets.fromLTRB(16.r, 11.5.r, 16.r, 8.r),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text("标题", style: TextStyle(color: MyColors.FF666666, fontSize: 14.sp),),
                        SizedBox(height: 8.r,),
                        TextField(
                          controller: logic.titleCon,
                          decoration: InputDecoration(
                            // 去除内边距
                            contentPadding: EdgeInsets.zero,
                            // 去掉底部下划线
                            border: InputBorder.none,
                            hintText: "一句话描述违规内容",
                            hintStyle: TextStyle(color: MyColors.FFBBBBBB, fontSize: 13.sp),
                            helperStyle: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),
                          ),
                          // 光标颜色
                          cursorColor: MyColors.FF010101,
                          // 光标宽度
                          cursorWidth: 1,
                          minLines: 1,
                          maxLines: 5,
                          maxLength: 50,
                          autofocus: false, // 自动对焦
                          style: TextStyle(color: MyColors.FF010101, fontSize: 14.sp, fontWeight: FontWeight.w500),
                          onChanged: (text) {
                            logic.updateData();
                          },
                        ),
                      ],
                    ),
                  ),
                  Container(
                    width: Get.width,
                    decoration: BoxDecoration(
                        color: Colors.white
                    ),
                    margin: EdgeInsets.only(top: 8.r, bottom: 8.r),
                    padding: EdgeInsets.fromLTRB(16.r, 11.5.r, 16.r, 8.r),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text("详细说明", style: TextStyle(color: MyColors.FF666666, fontSize: 14.sp),),
                        SizedBox(height: 12,),
                        TextField(
                          controller: logic.contentCon,
                          decoration: InputDecoration(
                            // 去除内边距
                            contentPadding: EdgeInsets.zero,
                            // 去掉底部下划线
                            border: InputBorder.none,
                            hintText: "详细注明发现问题的时间、地点、涉及人员等相关信息，便于效能办查证",
                            hintStyle: TextStyle(color: MyColors.FFBBBBBB, fontSize: 13),
                            helperStyle: TextStyle(color: MyColors.FF999999, fontSize: 11),
                          ),
                          // 光标颜色
                          cursorColor: MyColors.FF010101,
                          // 光标宽度
                          cursorWidth: 1,
                          minLines: 1,
                          maxLines: 5,
                          maxLength: 500,
                          autofocus: false, // 自动对焦
                          style: TextStyle(color: MyColors.FF010101, fontSize: 14, fontWeight: FontWeight.w500),
                          onChanged: (text) {
                            logic.updateData();
                          },
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            onTap: () {
              // 空白处取消TextField焦点
              FocusScopeNode currentFocus = FocusScope.of(context);
              if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
                FocusManager.instance.primaryFocus!.unfocus();
              }
            },
          ),
          bottomNavigationBar: Container(
            decoration: BoxDecoration(
                boxShadow: [BoxShadow(
                  color: Color(0x1a000000),
                  blurRadius: 6.67.r,
                  spreadRadius: 0.05.r,
                )],
                shape: BoxShape.rectangle,
                color: Colors.white
            ),
            padding: EdgeInsets.fromLTRB(16.r, 5.r, 16.r, 5.r),
            child: SafeArea(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Obx(() {
                    return BrnRadioButton(
                      radioIndex: 0,
                      isSelected: state.isSelected.value,
                      childOnRight: true,
                      mainAxisSize: MainAxisSize.max,
                      child: RichText(
                        text: TextSpan(
                            children: [
                              TextSpan(
                                text: '我已阅读《',
                                style: TextStyle(
                                    color: MyColors.FF333333, fontSize: 12.sp),
                              ),
                              TextSpan(
                                  text: '随手拍问题上报注意事项',
                                  style: TextStyle(
                                      color: MyColors.FF2763B9, fontSize: 12.sp),
                                  recognizer: TapGestureRecognizer()
                                    ..onTap = () {
                                      logic.goToWebViewPage();
                                    }),
                              TextSpan(
                                text: '》，承诺材料真实性',
                                style: TextStyle(
                                    color: MyColors.FF333333, fontSize: 12.sp),
                              ),
                            ]
                        ),
                      ),
                      onValueChangedAtIndex: (index, value) {
                        if (state.isSelected.value) {
                          state.isSelected.value = false;
                        } else {
                          state.isSelected.value = true;
                        }
                      },
                    );
                  }),
                  SizedBox(height: 5.r,),
                  Center(
                    child: Container(
                      child: SizedBox(
                        height: 40.r,
                        width: Get.width,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            primary: MyColors.FF2763B9,
                          ),
                          onPressed: () {
                            logic.saveOtherProblemSubmit();
                          },
                          child: Text("立即上报", style: TextStyle(color: Colors.white, fontSize: 14.sp)),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      );
    });
  }
}

